﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Configuration;

namespace NewsSpider.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2012，上海云里网络科技有限公司
    /// 内容摘要: 日志记录类
    /// 完成日期：2011年4月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class Log
    {
        /// <summary>
        /// 保存日志信息：异常错误、日常错误等
        /// </summary>
        /// <param name="strMessage">错误信息</param>
        public static void SaveErrorLog(string strMessage)
        {
            try
            {
                string strPath = Application.StartupPath;
                string strNewPath = strPath + "\\log\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(strNewPath))
                {
                    Directory.CreateDirectory(strNewPath);
                }
                string strFileName = strNewPath + "\\log.txt";

                FileInfo fi = new FileInfo(strFileName);
                if (fi.Exists && fi.Length > 2048 * 1024)
                {
                    fi.Delete();
                }
                FileStream fs = fi.OpenWrite();

                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write("日志时间：" + DateTime.Now.ToString() + "\r\n" + strMessage + "\r\n\r\n");
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }

        /// <summary>
        /// 保存抓取网址状态列表
        /// </summary>
        /// <param name="strMessage">信息</param>
        /// <param name="strFileTypeName">文件类型名称</param>
        public static void SaveHTMLLog(string strMessage, string strFileTypeName)
        {
            try
            {
                string strPath = Application.StartupPath;
                string strNewPath = strPath + "\\log\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(strNewPath))
                {
                    Directory.CreateDirectory(strNewPath);
                }
                string strFileName = strNewPath + "\\" + strFileTypeName + ".txt";

                FileInfo fi = new FileInfo(strFileName);
                if (fi.Exists && fi.Length > 2048 * 1024)
                {
                    fi.Delete();
                }
                FileStream fs = fi.OpenWrite();
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage + "\r\n");
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }

        /// <summary>
        /// 保存关键词信息到txt文件中
        /// </summary>
        /// <param name="strMessage">关键词信息</param>
        /// <param name="strFileName">文件名</param>
        public static void SaveKeywords(string strMessage, string strFileName)
        {
            try
            {
                string strPath = Application.StartupPath;
                string strFilePath = strPath + @"\\data\\" + strFileName + ".txt";

                FileInfo fi = new FileInfo(strFilePath);
                if (fi.Exists && fi.Length > 2048 * 1024)
                {
                    fi.Delete();
                }
                FileStream fs = fi.OpenWrite();

                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }

        public static void SaveSplitFile(string strMessage, string strFileName,string strFilePath)
        {
            try
            {
                strFilePath = strFilePath + "\\" + strFileName + ".txt";

                FileInfo fi = new FileInfo(strFilePath);
                FileStream fs = fi.OpenWrite();
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }

        /// <summary>
        /// 保存目标信息到txt文件中
        /// </summary>
        /// <param name="strMessage">信息</param>
        /// <param name="strFileName">文件名</param>
        /// <param name="strSaveTxtPath">路径</param>
        public static void SaveTxtFile(string strMessage, string strFileName,string strSaveTxtPath)
        {
            try
            {
                string strFilePath = strSaveTxtPath + strFileName + ".txt";

                FileInfo fi = new FileInfo(strFilePath);
                if (fi.Exists && fi.Length > 2048 * 1024)
                {
                    fi.Delete();
                }
                FileStream fs = fi.OpenWrite();

                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }
       

        /// <summary>
        /// 保存严重错误日志
        /// </summary>
        /// <param name="strMessage">错误信息</param>
        public static void SaveSpecialErrorLog(string strMessage)
        {
            try
            {
                ///指定日志文件的目录
                string strPath = Application.StartupPath;

                string strNewPath = strPath + "\\log\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(strNewPath))
                {
                    Directory.CreateDirectory(strNewPath);
                }
                string strFileName = strNewPath + "\\error.txt";

                ///定义文件信息对象
                FileInfo finfo = new FileInfo(strFileName);

                ///判断文件是否存在以及是否大于2K
                if (finfo.Exists && finfo.Length > 2048 * 1024)
                {
                    ///删除该文件
                    finfo.Delete();
                }
                ///创建只写文件流
                FileStream fs = finfo.OpenWrite();

                ///根据上面创建的文件流创建写数据流
                StreamWriter w = new StreamWriter(fs);

                ///设置写数据流的起始位置为文件流的末尾
                w.BaseStream.Seek(0, SeekOrigin.End);

                w.Write("日志时间：" + DateTime.Now.ToString() + "\r\n" + strMessage + "\r\n\r\n");

                ///清空缓冲区内容，并把缓冲区内容写入基础流
                w.Flush();

                ///关闭写数据流
                w.Close();
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
        }


        public static void SaveIndustryInfo(string strMessage, string strSaveTxtPath)
        {
            try
            {
                string strFilePath = strSaveTxtPath + ".txt";

                FileInfo fi = new FileInfo(strFilePath);
                FileStream fs = fi.OpenWrite();
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }


        /// <summary>
        /// 保存电子邮件发布状态列表
        /// </summary>
        /// <param name="strMessage">信息</param>
        /// <param name="strFileTypeName">文件类型名称</param>
        public static void SavePostLog(string strMessage, string strFileTypeName)
        {
            try
            {
                string strPath = Application.StartupPath;
                string strNewPath = strPath + "\\log\\" + DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(strNewPath))
                {
                    Directory.CreateDirectory(strNewPath);
                }
                string strFileName = strNewPath + "\\" + strFileTypeName + ".txt";

                FileInfo fi = new FileInfo(strFileName);
                if (fi.Exists && fi.Length > 2048 * 1024)
                {
                    fi.Delete();
                }
                FileStream fs = fi.OpenWrite();
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.Write(strMessage + "\r\n");
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
            }
        }
    }
}
